package com.yqg.manage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.controller.dto.BoothLicenceDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IBoothLicenceService;
import com.yqg.manage.entity.BoothLicence;

import org.springframework.web.bind.annotation.RestController;

/**
 * BoothLicenceController 前端控制器
 *
 * @author zxf
 * @since 2024-06-14
 */
@RestController
@RequestMapping("/booth-licence")
public class BoothLicenceController {

    @Resource
    private IBoothLicenceService boothLicenceService;

    @Value("${upload.path}")
    private String path;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者更新许可证")
    public Result save(@RequestBody BoothLicenceDto boothLicenceDto) {
        BoothLicence boothLicence = new BoothLicence();
        BeanUtils.copyProperties(boothLicenceDto, boothLicence);

        if (boothLicenceDto.getId() == null) { //新增
            boothLicenceService.saveOrUpdate(boothLicence);
            String pdfName = "api/file/download/" + path + "临时摊位许可证" + boothLicence.getId() + ".pdf";
            boothLicence.setPdf(pdfName);
            boothLicence.setCreateTime(new Date());
        }
        boolean b = boothLicenceService.saveOrUpdate(boothLicence);
        if (b) {
            return Result.success(boothLicence);
        }else {
            return Result.error();
        }

    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    @SysLog("删除许可证")
    public Result delete(@PathVariable Integer id) {
        BoothLicence boothLicence = boothLicenceService.getById(id);
        boothLicence.setIsDelete("1");
        return Result.success(boothLicenceService.saveOrUpdate(boothLicence));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<BoothLicence> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<BoothLicence> list = boothLicenceService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(boothLicenceService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam String boothId,
                           @RequestParam String boothName,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<BoothLicence> queryWrapper = new QueryWrapper<>();
        if (boothId != null && !boothId.isEmpty()) {
            queryWrapper.like("booth_id", boothId);
        }
        if (boothName != null && !boothName.isEmpty()) {
            queryWrapper.like("booth_name", boothName);
        }
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        Page<BoothLicence> page = boothLicenceService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<BoothLicence> boothLicenceList = page.getRecords();
        for (BoothLicence boothLicence : boothLicenceList) {
            boothLicence.setIssueTime(boothLicence.getIssueTimeYear() + "-" + boothLicence.getIssueTimeMouth() + "-" + boothLicence.getIssueTimeDay());
            boothLicence.setValidity(boothLicence.getValidityYear() + "-" + boothLicence.getValidityMouth() + "-" + boothLicence.getValidityDay());
        }
        return Result.success(page);
    }

}

